<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入标题" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="摘要" prop="summry">
        <el-input v-model="queryParams.summry" placeholder="请输入摘要" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="作者" prop="author">
        <el-input v-model="queryParams.author" placeholder="请输入作者" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="文件名" prop="fileName">
        <el-input v-model="queryParams.fileName" placeholder="请输入文件名" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="文件地址" prop="fileUrl">
        <el-input v-model="queryParams.fileUrl" placeholder="请输入文件地址" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="用户类型" prop="userType">
        <el-select v-model="queryParams.userType" placeholder="请选择用户类型" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="分类类型" prop="cateType">
        <el-select v-model="queryParams.cateType" placeholder="请选择分类类型" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="分类id" prop="cateId">
        <el-input v-model="queryParams.cateId" placeholder="请输入分类id" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="分类名称" prop="cateName">
        <el-input v-model="queryParams.cateName" placeholder="请输入分类名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="阅读统计" prop="readCount">
        <el-input v-model="queryParams.readCount" placeholder="请输入阅读统计" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="上线类型，0下线,1上线" prop="onlineType">
        <el-select v-model="queryParams.onlineType" placeholder="请选择上线类型，0下线,1上线" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="来源" prop="source">
        <el-input v-model="queryParams.source" placeholder="请输入来源" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="工作单位" prop="workUnit">
        <el-input v-model="queryParams.workUnit" placeholder="请输入工作单位" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="发布时间">
        <el-date-picker v-model="dateRangePushTime" style="width: 240px" value-format="yyyy-MM-dd"
                        type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" />
      </el-form-item>
      <el-form-item label="标签" prop="tag">
        <el-input v-model="queryParams.tag" placeholder="请输入标签" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="权重越大越靠前" prop="weight">
        <el-input v-model="queryParams.weight" placeholder="请输入权重越大越靠前" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="删除状态 0未删除，1已删除" prop="isDelete">
        <el-input v-model="queryParams.isDelete" placeholder="请输入删除状态 0未删除，1已删除" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker v-model="dateRangeCreateTime" style="width: 240px" value-format="yyyy-MM-dd"
                        type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['nkycontent:artile:create']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" :loading="exportLoading"
                   v-hasPermi="['nkycontent:artile:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="摘要" align="center" prop="summry" />
      <el-table-column label="内容" align="center" prop="content" />
      <el-table-column label="作者" align="center" prop="author" />
      <el-table-column label="文件名" align="center" prop="fileName" />
      <el-table-column label="文件地址" align="center" prop="fileUrl" />
      <el-table-column label="封面" align="center" prop="coverImage" />
      <el-table-column label="用户类型" align="center" prop="userType" />
      <el-table-column label="分类类型" align="center" prop="cateType" />
      <el-table-column label="分类id" align="center" prop="cateId" />
      <el-table-column label="分类名称" align="center" prop="cateName" />
      <el-table-column label="阅读统计" align="center" prop="readCount" />
      <el-table-column label="上线类型，0下线,1上线" align="center" prop="onlineType" />
      <el-table-column label="来源" align="center" prop="source" />
      <el-table-column label="工作单位" align="center" prop="workUnit" />
      <el-table-column label="发布时间" align="center" prop="pushTime" />
      <el-table-column label="标签" align="center" prop="tag" />
      <el-table-column label="权重越大越靠前" align="center" prop="weight" />
      <el-table-column label="删除状态 0未删除，1已删除" align="center" prop="isDelete" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['nkycontent:artile:update']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['nkycontent:artile:delete']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="摘要" prop="summry">
          <el-input v-model="form.summry" placeholder="请输入摘要" />
        </el-form-item>
        <el-form-item label="内容">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-form-item label="作者" prop="author">
          <el-input v-model="form.author" placeholder="请输入作者" />
        </el-form-item>
        <el-form-item label="文件名" prop="fileName">
          <el-input v-model="form.fileName" placeholder="请输入文件名" />
        </el-form-item>
        <el-form-item label="文件地址" prop="fileUrl">
          <el-input v-model="form.fileUrl" placeholder="请输入文件地址" />
        </el-form-item>
        <el-form-item label="用户类型" prop="userType">
          <el-select v-model="form.userType" placeholder="请选择用户类型">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="分类类型" prop="cateType">
          <el-select v-model="form.cateType" placeholder="请选择分类类型">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="分类id" prop="cateId">
          <el-input v-model="form.cateId" placeholder="请输入分类id" />
        </el-form-item>
        <el-form-item label="分类名称" prop="cateName">
          <el-input v-model="form.cateName" placeholder="请输入分类名称" />
        </el-form-item>
        <el-form-item label="阅读统计" prop="readCount">
          <el-input v-model="form.readCount" placeholder="请输入阅读统计" />
        </el-form-item>
        <el-form-item label="上线类型，0下线,1上线" prop="onlineType">
          <el-select v-model="form.onlineType" placeholder="请选择上线类型，0下线,1上线">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="来源" prop="source">
          <el-input v-model="form.source" placeholder="请输入来源" />
        </el-form-item>
        <el-form-item label="工作单位" prop="workUnit">
          <el-input v-model="form.workUnit" placeholder="请输入工作单位" />
        </el-form-item>
        <el-form-item label="发布时间" prop="pushTime">
          <el-date-picker clearable v-model="form.pushTime" type="date" value-format="timestamp" placeholder="选择发布时间" />
        </el-form-item>
        <el-form-item label="标签" prop="tag">
          <el-input v-model="form.tag" placeholder="请输入标签" />
        </el-form-item>
        <el-form-item label="权重越大越靠前" prop="weight">
          <el-input v-model="form.weight" placeholder="请输入权重越大越靠前" />
        </el-form-item>
        <el-form-item label="删除状态 0未删除，1已删除" prop="isDelete">
          <el-input v-model="form.isDelete" placeholder="请输入删除状态 0未删除，1已删除" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { createArtile, updateArtile, deleteArtile, getArtile, getArtilePage, exportArtileExcel } from "@/api/nkycontent/artile";
import Editor from '@/components/Editor';

export default {
  name: "Artile",
  components: {
    Editor,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 新闻列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      dateRangePushTime: [],
      dateRangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        title: null,
        summry: null,
        content: null,
        author: null,
        fileName: null,
        fileUrl: null,
        coverImage: null,
        userType: null,
        cateType: null,
        cateId: null,
        cateName: null,
        readCount: null,
        onlineType: null,
        source: null,
        workUnit: null,
        tag: null,
        weight: null,
        isDelete: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        isDelete: [{ required: true, message: "删除状态 0未删除，1已删除不能为空", trigger: "blur" }],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 处理查询参数
      let params = {...this.queryParams};
      this.addBeginAndEndTime(params, this.dateRangePushTime, 'pushTime');
      this.addBeginAndEndTime(params, this.dateRangeCreateTime, 'createTime');
      // 执行查询
      getArtilePage(params).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        title: undefined,
        summry: undefined,
        content: undefined,
        author: undefined,
        fileName: undefined,
        fileUrl: undefined,
        coverImage: undefined,
        userType: undefined,
        cateType: undefined,
        cateId: undefined,
        cateName: undefined,
        readCount: undefined,
        onlineType: undefined,
        source: undefined,
        workUnit: undefined,
        pushTime: undefined,
        tag: undefined,
        weight: undefined,
        isDelete: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRangePushTime = [];
      this.dateRangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加新闻";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getArtile(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改新闻";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          updateArtile(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        createArtile(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认删除新闻编号为"' + id + '"的数据项?').then(function() {
          return deleteArtile(id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = {...this.queryParams};
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.addBeginAndEndTime(params, this.dateRangePushTime, 'pushTime');
      this.addBeginAndEndTime(params, this.dateRangeCreateTime, 'createTime');
      // 执行导出
      this.$modal.confirm('是否确认导出所有新闻数据项?').then(() => {
          this.exportLoading = true;
          return exportArtileExcel(params);
        }).then(response => {
          this.$download.excel(response, '新闻.xls');
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
